package seap.dao;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.*;

public class HibernateFactory {
    private static SessionFactory sessionFactory;
    private static Log log = LogFactory.getLog(HibernateFactory.class);

//*********************************************************************************************************************
//*  Constructors/Factory Methods
//*********************************************************************************************************************
    /**
     * Constructs a new Singleton SessionFactory
     * @return
     * @throws HibernateException
     */
//    public static SessionFactory buildSessionFactory() throws HibernateException {
//        if (sessionFactory != null) {
//            closeFactory();
//        }
//        return configureSessionFactory();
//    }

    /**
     * Builds a SessionFactory, if it hasn't been already.
     */
    /**
     * Constructs a new Singleton SessionFactory
     * @return
     * @throws HibernateException
     */
    public static SessionFactory buildSessionFactory() throws HibernateException{
        if (sessionFactory != null) {
            return sessionFactory;
        }
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            sessionFactory = configuration.buildSessionFactory();
            return sessionFactory;
        } catch (HibernateException e) {
            throw e;
        }
    }
//*********************************************************************************************************************
//*  Accessor Methods
//*********************************************************************************************************************
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
//*********************************************************************************************************************
//*  Public Methods
//*********************************************************************************************************************

    public static Session openSession() throws HibernateException {
    	buildSessionFactory();
    	Session session = sessionFactory.openSession();
        return session;
    }

    public static void closeFactory() {
        if (sessionFactory != null) {
            try {
                sessionFactory.close();
            } catch (HibernateException ignored) {
                log.error("Couldn't close SessionFactory", ignored);
            }
        }
    }

    public static void close(Session session) {
        if (session != null) {
            try {
                session.close();
            } catch (HibernateException ignored) {
                log.error("Couldn't close Session", ignored);
            }
        }
    }

    public static void rollback(Transaction tx) {
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (HibernateException ignored) {
            log.error("Couldn't rollback Transaction", ignored);
        }
    }
}    

